//
//  RightSearchSonViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/7.
//  Copyright © 2016年 LST. All rights reserved.
//  社区

#import "RightSearchSonViewController.h"
#import "RightSearchSonTableViewCell.h"
#import "AFNetworking.h"
#import "RootModel.h"
#import "Projects.h"
#import "User.h"
#import "UserImage.h"
#import "CommunityViewController.h"
#import "DecorateSonViewController.h"
#import "WQLayout.h"
#import "RightHeaderTableViewCell.h"
#define Width [UIScreen mainScreen].bounds.size.width
@interface RightSearchSonViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
/**
 *  无限轮播要使用的数组
 */
@property (nonatomic, strong) NSMutableArray *bannerImageArray;

/**
 *  真实数量的图片数组
 */
@property (nonatomic, strong) NSMutableArray *imageArray;

@end

@implementation RightSearchSonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (int index = 0; index < 5; index++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",index]];
        [self.imageArray addObject:image];
    }
    for (NSInteger imageIndex = 0; imageIndex < 3; imageIndex ++) {
        [self.bannerImageArray addObjectsFromArray:self.imageArray];
    }
    // 左
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 50, 30);
    [leftBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    // 右
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 50, 30);
    [rightBtn setTitle:@"社区" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
    [self handData];
    [self creatTableView];
}
- (void)handData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://api.guju.com.cn/v2/building/matchProject?roomStyleId=332501&start=1&count=5&userId=%@", _urlStr] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.rootModel = [RootModel modelWithDic:dic];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
- (void)creatCycleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"RightSearchSonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
   // [_tableView registerClass:[RightHeaderTableViewCell class] forCellReuseIdentifier:@"headerCell"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DecorateSonViewController *sonVC = [[DecorateSonViewController alloc] init];
    sonVC.hidesBottomBarWhenPushed = YES;
    sonVC.urlId = _rootModel.projects[indexPath.row].Son_id.description;
    [self.navigationController pushViewController:sonVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rootModel.projects.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        RightHeaderTableViewCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell"];
//        return headerCell;
//    } else {
    RightSearchSonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.projects = _rootModel.projects[indexPath.row];
    return cell;
    //}
}
- (void)clickLeft:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)clickRight:(UIBarButtonItem *)sender {
    CommunityViewController *communityVC = [[CommunityViewController alloc] init];
    
    [self.navigationController pushViewController:communityVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
